package com.tywj.navigation.base

import android.os.Bundle
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MyLocationStyle
import com.tywj.panda.customer.common.tool.PermissionTool
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import com.tywj.lib.location.entity.LocationEntity
import com.tywj.navigation.R
import com.tywj.navigation.tool.MapTool

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/05/14
 *     desc   : 地图Activity基类
 *     version: 2.0.0
 * </pre>
 */
abstract class BaseMapActivity : MyBaseActivity() {

    // 高德地图map
    public lateinit var map: AMap

    private val mapView: MapView by lazy { findMapView() }

    private lateinit var myLocationStyle: MyLocationStyle

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        mapView.onCreate(savedInstanceState)
        map = mapView.map
        myLocationStyle = MapTool.getMyLocationStyle()

    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        map.uiSettings.let {
            // 显示高德地图的缩放按钮
            it.isZoomControlsEnabled = true
            // 显示高德地图的定位按钮
            it.isMyLocationButtonEnabled = true
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState)

    }


    override fun onResumeFirst() {
        super.onResumeFirst()
        PermissionTool.location(this) {}
    }

    override fun initListener() {
        super.initListener()

        // 更新用户当前位置
        map.setOnMyLocationChangeListener {
            if (it.latitude == 0.0 && it.longitude == 0.0) {
                return@setOnMyLocationChangeListener
            }
            MapTool.updateLocation(it)
            onMyLocationChange(MapTool.curLocation!!)
        }
    }

    override fun onResume() {
        super.onResume()

        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()

        mapView.onPause()
    }

    override fun onStop() {
        super.onStop()

        // 暂停定位
        map.isMyLocationEnabled = false
    }

    override fun onDestroy() {
        super.onDestroy()

        mapView.onDestroy()
    }

    override fun onResumeEveryTime() {
        super.onResumeEveryTime()

        // 设置定位蓝点的Style
        map.myLocationStyle = myLocationStyle
        // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false
        map.isMyLocationEnabled = true
    }


    protected fun toLocation(latitude: Double, longitude: Double, zoom: Float) {
        val cameraPosition = CameraPosition.Builder()
            .target(LatLng(latitude, longitude, true))
            .zoom(zoom)
            .build()
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
    }

    protected open fun onMyLocationChange(location: LocationEntity) {}

    protected  open fun findMapView(): MapView = findViewById(R.id.navView)

}